package cn.liyunqi.ttv.start.util;

import cn.liyunqi.core.exception.BizException;
import cn.liyunqi.core.util.StringUtil;
import cn.liyunqi.ttv.start.view.AlertUtil;
import javafx.application.Platform;
import javafx.stage.Stage;

import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class CommonUtil {

    private static final String mainUrl = "file:liyunqi-ttv-main.jar";

    public static boolean isWindows() {
        return System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1;
    }

    public static void wakeUpMain(String userId,String mail, String token,Long limitTime) {
        try {

            URL url = new URL(mainUrl);

            String callingClassName = "cn.liyunqi.ttv.main.StartApp";
            //ClassLoader a = Thread.currentThread().getContextClassLoader();

            URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url}, Thread.currentThread().getContextClassLoader());

            Thread.currentThread().setContextClassLoader(urlClassLoader);

            //Thread.currentThread().getContextClassLoader()
            Class<?> clazz = urlClassLoader.loadClass(callingClassName);


            Method method = clazz.getMethod("start",Stage.class,String.class,String.class,String.class,Long.class);
            Object result = method.invoke(clazz.newInstance(), new Stage(), userId,mail, token,limitTime);
            if(!StringUtil.isEmptyOrNull(result)){
                throw new BizException(result.toString());
            }

        }catch (BizException ex){
            throw ex;
        }catch (Exception ex){
            ex.printStackTrace();
            throw new BizException("主程序唤醒失败");
        }
    }
}
